package org.devocean.groupbuying.web.frontend;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.devocean.groupbuying.dao.DealDAO;
import org.devocean.groupbuying.dao.ExploredDAO;
import org.devocean.groupbuying.domain.Deal;
import org.devocean.groupbuying.domain.Explored;
import org.devocean.groupbuying.domain.Media;
import org.devocean.groupbuying.domain.Plan;
import org.devocean.groupbuying.domain.Product;
import org.devocean.groupbuying.domain.Promotion;
import org.devocean.groupbuying.domain.Voucher;
import org.devocean.groupbuying.service.CategoryService;
import org.devocean.groupbuying.service.DealService;
import org.devocean.groupbuying.service.ExploredService;
import org.devocean.groupbuying.service.PlanService;
import org.devocean.groupbuying.service.ProductService;
import org.devocean.groupbuying.service.PromotionService;
import org.devocean.groupbuying.service.PurchaseService;
import org.devocean.groupbuying.service.VoucherService;
import org.devocean.groupbuying.web.frontend.displaybeans.DealBean;
import org.devocean.groupbuying.web.frontend.displaybeans.ExplorerBean;
import org.devocean.groupbuying.web.frontend.displaybeans.PurchaseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = { "/" })
public class HomeController {

	@Autowired
	private DealService dealService;

	@Autowired
	private DealDAO dealDAO;

	@Autowired
	private VoucherService voucherService;

	@Autowired
	private PlanService planService;

	@Autowired
	private PromotionService promotionService;

	@Autowired
	private ProductService productService;

	@Autowired
	private PurchaseService purchaseService;

	@Autowired
	private CategoryService categoryService;

	@Autowired
	private ExploredService exploredService;

	@RequestMapping("/")
	public String index(ModelMap map, Principal principal,
			@CookieValue("clientLanguage") String lang) {
		Integer max = 16;
		Integer index = 1;

		List<ExplorerBean> explorerBeans = new ArrayList<ExplorerBean>();

		int start = (int) ((index * max) - max);
		for (Explored ex : exploredService
				.getExploredsEntries(lang, start, max)) {
			explorerBeans.add(new ExplorerBean(ex));
		}
		long count = explorerBeans.size();
		map.put("count", count);
		map.put("categories", categoryService.getDisplayCategories(lang));
		map.put("explorerBeans", explorerBeans);
		return "index";

	}

	@RequestMapping("/cookie")
	public String index() {
		return "redirector";
	}

	@RequestMapping("error/{code}")
	public String error(ModelMap map, Principal principal,
			@PathVariable("code") String code,
			@CookieValue("clientLanguage") String lang) {

		map.put("msg", "Code :" + code + " : Page not found");

		return "error";
	}

	@RequestMapping("test/comissions")
	public String comissions(ModelMap map) {

		Plan p = planService.getPlanById(2183);

		// p.add(new Promotion(
		// p,
		// 0.,
		// "http://www.anrdoezrs.net/click-5605698-10751179",
		// " $50 OFF FOR 1 YEAR E-COMMERCE HOSTING PLAN",
		// "Get instant $50 off for E-Commerce Hosting Plan signups. Limited Offer!",
		// 0., 0, new Date(2015, 06, 19), new Date(2012, 06, 19),
		// "discount"));
		p.add(new Promotion(
				p,
				0.,
				"http://www.anrdoezrs.net/click-5605698-10751179",
				" $50 OFF FOR 1 YEAR E-COMMERCE HOSTING PLAN",
				"Get instant $50 off for E-Commerce Hosting Plan signups. Limited Offer!",
				0., 0, new Date(2015, 06, 19), new Date(2012, 06, 19),
				"discount"));

		planService.addOrUpdatePlan(p);

		map.put("msg", "commissions added for " + p.toString());
		return "error";
	}

	@RequestMapping("test/purchases")
	public String testp(ModelMap modelMap, Principal p) {

		// Deal deal = dealService.getDealById(1); //

		//
		// Voucher voucher = voucherService.getVoucherById(1);
		//
		// voucher.getSerialKeys()
		// .add(new SerialKey("huhuipiuiug", true, voucher));
		// voucher.getSerialKeys().add(new SerialKey("dqfhdqh", true, voucher));
		// voucher.getSerialKeys().add(
		// new SerialKey("huhuipqdfhiuiug", true, voucher));
		// voucher.getSerialKeys().add(new SerialKey("dsfgsdfg", true,
		// voucher));
		// voucher.getSerialKeys().add(new SerialKey("sdfgsdfsg", true,
		// voucher));

		// voucherService.addOrUpdateVoucher(voucher);

		// dealDAO.updateDeal(deal);
		// for (int i = 0; i < 10; i++) {
		// Deal deal = dealDAO.findDealWithKey(1, "en");
		// System.out.println(deal.getDiscounts());
		// }

		// System.out.println(p);
		PurchaseBean purchaseBean = new PurchaseBean(
				purchaseService.getPurchaseById(1));
		modelMap.addAttribute("purchaseBean", purchaseBean);
		// mailService.confirmPayment(purchaseService.getPurchaseById(10));
		return "payment/payed";
	}

	@RequestMapping("test")
	public String test(ModelMap modelMap) {
		modelMap.addAttribute("voucher", voucherService.getVoucherById(1));
		return "test";
	}

	@RequestMapping("test/test")
	public String testPost(ModelMap modelMap) {
		for (Product product : productService.getAllProducts()) {
			System.out.println(product.getProductID());
			System.out.println("Starting...");
			exploredService.addOrUpdateExplored(product);

			System.out.println("Done");
		}
		return "#";
	}

	@RequestMapping("test/exp")
	public String testexp(ModelMap modelMap) {

		List<Explored> list = exploredService.getAllExploreds();

		for (Explored explored : list) {
			Boolean deals = false;
			Boolean promotions = false;
			Product p = explored.getProduct();
			for (Deal deal : p.getDeals()) {
				if (deal.getStatus().equals("pending")
						|| deal.getStatus().equals("active")) {
					deals = true;
					System.out.println("deals found");
				}
			}

			for (Plan plan : p.getPlans()) {
				for (Promotion promotion : plan.getPromotions()) {
					if (Calendar.getInstance().getTime()
							.before(promotion.getExpireDate())) {
						promotions = true;
						System.out.println("promotions found");
					}
				}
			}

			if (deals || promotions) {
				System.out.println("Deals ON");
				// explored.setHasDeals(true);
			} else {
				System.out.println("Deals OFF");
				// explored.setHasDeals(false);
			}
			exploredService.addOrUpdateExplored(explored);

		}

		return "#";
	}
}
